/*!
 Temelia - B-tree performance tests.

 Copyright (C) 2008 Ceata (http://cod.ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"

extern "C"
{
#include <temelia/b_tree.h>
#include <temelia/common.h>
}

static volatile int __degree;

static b_tree_t __temelia_b_tree_insert(int n)
{
	b_tree_t btree;

	btree = b_tree_new(__degree);

	for (int i = 0; i < n; i++)
		b_tree_insert(btree, default_collection[i], int_compare, NULL, 1);

	return btree;
}

static void temelia_b_tree_insert(int n)
{
	b_tree_delete(__temelia_b_tree_insert(n));
}

static void temelia_b_tree_search(int n)
{
	b_tree_t btree;

	btree = __temelia_b_tree_insert(n);

	for (int i = 0; i < n; i++)
		b_tree_search(btree, default_collection[i], int_compare, NULL);

	b_tree_delete(btree);
}

static void temelia_b_tree_remove(int n)
{
	b_tree_t btree;

	btree = __temelia_b_tree_insert(n);

	for (int i = 0; i < n; i++)
		b_tree_remove(btree, default_collection[i], int_compare, NULL);

	b_tree_delete(btree);
}

void TemeliaPerformance::pt_b_tree(void)
{
	TestUnit testUnit("b_tree");

	__degree = 4;
	GENERATE_TEMELIA_TEST(testUnit, "insert_4", b_tree_insert);
	GENERATE_TEMELIA_TEST(testUnit, "search_4", b_tree_search);
	GENERATE_TEMELIA_TEST(testUnit, "remove_4", b_tree_remove);

	__degree = 16;
	GENERATE_TEMELIA_TEST(testUnit, "insert_16", b_tree_insert);
	GENERATE_TEMELIA_TEST(testUnit, "search_16", b_tree_search);
	GENERATE_TEMELIA_TEST(testUnit, "remove_16", b_tree_remove);

	testUnit.write(fout);
}
